home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earkit / news / thor / rexx / showhtml.thor < prev    next >
Text File  |  1998-05-24  |  8KB  |  347 lines

  1. /*
  2. ** $VER: ShowHTML.thor 1.2 (22.9.97)
  3. **
  4. ** by Eirik Nicolai Synnes <eirikns@ifi.uio.no>
  5. **
  6. ** ShowHTML.thor will send a HTML document in the message currently displayed
  7. ** in Thor's main window to a web browser.  First it searches for a browser
  8. ** already in memory and uses this one, uniconifying it if necessary.  If no
  9. ** browser is active it will launch the browser configured using CfgHTTP.thor.
  10. **
  11. ** Currenly ShowHTML.thor recognizes IBrowse, AWeb, Voyager and AMosaic.
  12. **
  13. **
  14. ** Fixed in 1.2:
  15. **
  16. **   o If ShowHTML wanted to display a requestor it would fail with
  17. **     an ARexx error
  18. **
  19. **
  20. ** New in 1.1:
  21. **
  22. **   o HTML search routines vastly improved
  23. **   o Added support for Voyager (2.88 tested, might not work with earlier
  24. **     versions)
  25. **   o Added support for AMosaic (not tested)
  26. **   o Now uses CfgHTTP.thor's configuration file to figure out how to start
  27. **     the browser
  28. **   o Browser window is always brought to front and activated (if the
  29. **     browser's ARexx port support it)
  30. **   o Lots of minor enhancements and bug fixes
  31. **
  32. **
  33. ** Todo:
  34. **
  35. **   o Delete shows "Delete returned 20" if it couldn't delete the temporary
  36. **     file. Is it possible to get rid of this?
  37. **
  38. **   o See if it is possible to wait for something in order to avoid
  39. **     temp file getting deleted before the browser has loaded it
  40. **
  41. **   o Add a WaitForPort after running browser
  42. **
  43. **   o Figure out a flexible method to support inline pictures
  44. **        (
  45. **          Netscape:
  46. **          <IMG SRC="cid:picid">
  47. **          Picture attachment has Content-ID:<picid>
  48. **        )
  49. **
  50. */
  51.  
  52. options results
  53. options failat 31
  54.  
  55. signal on break_c
  56. signal on halt
  57. signal on error
  58.  
  59. globals = 'fileopen filename outfile THOR.LASTERROR BBSREAD.LASTERROR thorport msgtext. wwwcmd wwwport globals'
  60.  
  61. fileopen = 0
  62. filename = 'T:SaveHTML.' || pragma('ID') || '.html'
  63.  
  64.  
  65. /*
  66. ** See if I'm run from Thor
  67. */
  68.  
  69. if (left(address(), 5) = 'THOR.') then thorport = address()
  70. else do
  71.     say 'This script must be run from Thor.'
  72.     exit(20)
  73.     end
  74.  
  75.  
  76. /*
  77. ** Find/open BBSREAD ARexx port
  78. */
  79.  
  80. if ~(show('P', 'BBSREAD')) then do
  81.     address(command)
  82.     'Run >NIL: `GetEnv THOR/THORPath`bin/LoadBBSRead'
  83.     'WaitForPort BBSREAD'
  84.     if (rc ~= 0) then displayerror(30, 'SortMail', 'Couldn''t open BBSREAD''s ARexx port.')
  85.     end
  86.  
  87. call loadprefs()
  88.  
  89. /*
  90. ** Read the current message
  91. */
  92.  
  93. address(thorport)
  94. 'CURRENTMSG STEM 'curmsg
  95. if (rc ~= 0) then call fail('Couldn''t detect a current message.')
  96.  
  97. address(bbsread)
  98. 'READBRMESSAGE "'curmsg.BBSNAME'" "'curmsg.CONFNAME'" 'curmsg.MSGNR' TEXTSTEM 'msgtext
  99. if (rc ~= 0) then call fail('Couldn''t read message:\n'BBSREAD.LASTERROR)
  100.  
  101.  
  102. /*
  103. ** Find out what browser(s) is/are active
  104. */
  105.  
  106. call findbrowser()
  107.  
  108. /*
  109. ** Find a text/html part
  110. */
  111.  
  112. if ~(findhtml('msgtext')) then do
  113.     if (symbol('msgtext.TEXT.COUNT') = 'VAR') & (msgtext.TEXT.COUNT > 0) then do
  114.         address(thorport)
  115.         'REQUESTNOTIFY TEXT "No text/html message part found.\nDo you want to send the first\nmessage part to the browser?" BT "Yes|No"'
  116.         if (rc ~= 0) then do
  117.             say 'Couldn''t open requester: 'THOR.LASTERROR
  118.             exit(0)
  119.         end
  120.         if (result = 1) then call savemsg('msgtext')
  121.         else exit(0)
  122.     end
  123.     else fail('No text/html message part found.')
  124. end
  125.  
  126.  
  127. /*
  128. ** Display HTML document
  129. */
  130.  
  131. if symbol('wwwport') ~= 'VAR' then do
  132.     address command 'Run <NIL: >NIL: 'wwwcmd' file://localhost/' || filename
  133.     if rc ~= 0 then fail('Failed to run browser.')
  134. end
  135. else do
  136.     address(wwwport)
  137.     select
  138.         when wwwport = 'VOYAGER' then 'OPENURL file://localhost/' || filename
  139.         when wwwport = 'IBROWSE' then 'GOTOURL file://localhost/' || filename
  140.         when left(wwwport, 5) = 'AWEB.' then 'OPEN URL file://localhost/' || filename || ' RELOAD'
  141.         when left(wwwport, 8) = 'AMOSAIC.' then 'JUMP URL file://localhost/' || filename
  142.         otherwise nop
  143.     end
  144.     if (rc ~= 0) then call fail('Browser failed to display document.')
  145. end
  146.  
  147.  
  148. /*
  149. ** Activate browser window
  150. */
  151.  
  152. if (symbol('wwwport') ~= 'VAR') then call findbrowser
  153.  
  154. address(wwwport)
  155.  
  156. select
  157.     when (wwwport = 'VOYAGER') then do
  158.         'SHOW'
  159.         'ACTIVATE'
  160.     end
  161.     when (wwwport = 'IBROWSE') then do
  162.         'SHOW'
  163.         'SCREENTOFRONT'
  164.         'ACTIVATE'
  165.     end
  166.     when (left(wwwport, 5) = 'AWEB.') then do
  167.         'WINDOWTOFRONT'
  168.         'SCREENTOFRONT'
  169.         'ACTIVATEWINDOW'
  170.     end
  171.     when (left(wwwport, 8) = 'AMOSAIC.') then do
  172.         'SHOW'
  173.         'ACTIVATE'
  174.     end
  175.     otherwise nop
  176. end
  177.  
  178. /*
  179. ** Clean up and exit
  180. */
  181.  
  182. cleanup:
  183. break_c:
  184. halt:
  185. error:
  186.  
  187. if fileopen = 1 then call close(outfile)
  188.  
  189.  
  190. /*
  191. ** See if the file can be deleted. Checks every 10 seconds.
  192. */
  193.  
  194. if exists(filename) then do
  195.     address(command)
  196.     'Wait 20'
  197.     do i = 1 to 6
  198.         'Wait 10'
  199.         'Delete >NIL: "'filename'"'
  200.         if (rc = 0) then leave i
  201.     end
  202. end
  203.  
  204. exit(0)
  205.  
  206.  
  207. /****************************************************************************
  208. ********************************** Procedures ********************************
  209.  ***************************************************************************/
  210.  
  211.  
  212. /**
  213. *** Find the first text/html part
  214. **/
  215.  
  216. findhtml: interpret 'procedure expose 'globals
  217.           parse arg tstem
  218.  
  219. foundct = 0
  220.  
  221. if (symbol(tstem'.COMMENT.COUNT') = 'VAR') & (value(tstem'.COMMENT.COUNT') > 0) then do i = 1 to value(tstem'.COMMENT.COUNT') while foundct = 0
  222.     curline = upper(value(tstem'.COMMENT.i'))
  223.     if (subword(curline, 1, 1) = 'CONTENT-TYPE:') & (compress(subword(curline, 2, 1), ';') = 'TEXT/HTML') then foundct = 1
  224. end
  225.  
  226. if (upper(value(tstem'.BINARY.DESC')) = 'TEXT/HTML') then foundct = 1
  227.  
  228. if foundct = 1 then do
  229.     call savemsg(tstem)
  230.     return(1)
  231. end
  232. else if (symbol(tstem'.PART.COUNT') = 'VAR') & (value(tstem'.PART.COUNT') > 0) then do i = 1 to value(tstem'.PART.COUNT')
  233.     newstem = tstem || '.PART.' || i || '.MSG'
  234.     call findhtml(newstem)
  235.     if (result = 1) then return(1)
  236. end
  237.  
  238. return(0)
  239.  
  240.  
  241.  
  242. /**
  243. *** Save a messagepart to disk
  244. **/
  245.  
  246. savemsg: interpret 'procedure expose 'globals
  247.             parse arg htmltext
  248.  
  249. /* Write text body */
  250.  
  251. if (symbol(htmltext'.TEXT.COUNT') = 'VAR') then do
  252.     cnt = value(htmltext'.TEXT.COUNT')
  253.  
  254.     if (cnt > 0) then do
  255.         fileopen = open(outfile, filename, 'W')
  256.         if ~(fileopen) then do
  257.             call fail('Couldn''t open "' || filename || '" for writing.')
  258.             return(20)
  259.         end
  260.         do i = 1 to cnt
  261.             call writeln(outfile, value(htmltext'.TEXT.'i))
  262.         end
  263.         call close(outfile)
  264.     end
  265.  
  266.     else if (symbol(htmltext'.PART.1.BINARY') = 'VAR') & (value(htmltext'.PART.1.BINARY.DESC') = 'text/html') then do
  267.         htmlpath = value(htmltext'.PART.1.BINARY')
  268.         if ~(exists(htmlpath)) then fail('text/html part was deleted or not found.')
  269.         else address command 'Copy "'htmlpath'" TO "'filename'" QUIET'
  270.     end
  271.  
  272.     else fail('text/html part was empty.')
  273. end
  274.  
  275. return(0)
  276.  
  277.  
  278. /*
  279. ** Find an active browser, run one if none is found
  280. */
  281.  
  282. findbrowser: interpret 'procedure expose 'globals
  283.  
  284. /* Go through available ports */
  285.  
  286. ports = show('P')
  287.  
  288. do i = 1 to words(ports)
  289.     if left(subword(ports, i), 5) = 'AWEB.'    then wwwport = subword(ports, i, 1)
  290.     if left(subword(ports, i), 8) = 'AMOSAIC.' then wwwport = subword(ports, i, 1)
  291.     if left(subword(ports, i), 7) = 'VOYAGER'  then wwwport = subword(ports, i, 1)
  292.     if left(subword(ports, i), 7) = 'IBROWSE'  then wwwport = subword(ports, i, 1)
  293.     if symbol('wwwport') = 'VAR' then break
  294. end
  295.  
  296. if left(subword(ports, i), 5) = 'AWEB.' then do
  297.     address(wwwport)
  298.     'GET ACTIVEPORT'
  299.     if (rc = 0 ) then wwwport = result
  300. end
  301.  
  302. return(0)
  303.  
  304.  
  305. /*
  306. ** Display an error message and exit
  307. */
  308.  
  309. fail: interpret 'procedure expose 'globals
  310.       parse arg errtext
  311.  
  312. address(thorport)
  313.  
  314. 'REQUESTNOTIFY TEXT "'errtext'" BUTTONTEXT "Abort"'
  315. if (rc ~= 0) then do
  316.     say 'Couldn''t open error requester: 'THOR.LASTERROR
  317.     say 'Original error was: 'errtext
  318. end
  319.  
  320. signal cleanup
  321.  
  322.  
  323. /*
  324. ** Load preferences saved by CfgHTTP.thor
  325. */
  326.  
  327. loadprefs: interpret 'procedure expose 'globals
  328.  
  329. cfgfile = 'ENV:Thor/http.config'
  330.  
  331. if ~(exists(cfgfile)) then do
  332.     address(thorport)
  333.     'REQUESTNOTIFY TEXT "Could not find the configuration file.\nRun CfgHTTP to create one or quit." BT "CfgHTTP|Quit"'
  334.     if (rc = 0) & (result = 1) then address command 'rx `GetEnv THOR/THORPath`rexx/cfghttp.thor'
  335.     exit(0)
  336. end
  337. else do
  338.     call open(prf, cfgfile, 'R')
  339.     do until eof(prf)
  340.         line = readln(prf)
  341.         if upper(word(line, 1)) = 'BROWSEREXE' then wwwcmd = subword(line, 2)
  342.     end
  343.     call close(prf)
  344. end
  345.  
  346. return(0)
  347.